﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using System.Data.Entity;
using cs499;
using cs499.Database;

namespace cs499
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            //testnseed();

            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterOpenAuth();
            if (!Roles.RoleExists("admin"))
            {
                Roles.CreateRole("admin");
            }
            if (!Roles.RoleExists("faculty"))
            {
                Roles.CreateRole("faculty");
            }
            if(Membership.GetUser("admin")==null){
                Membership.CreateUser("admin","password");
                Roles.AddUserToRole("admin", "admin");
            }
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }

        void testnseed() {
            Department csdep = ListOfDepartment.CreateOrGetDepartment("Computer Science", "CS");
            if (csdep == null)throw new ArgumentNullException("ListOfDepartment.CreateOrGetDepartment","unable to seed cs dep");
            
            Department madep = ListOfDepartment.CreateOrGetDepartment("Mathematics", "MA");
            if (madep == null) throw new ArgumentNullException("ListOfDepartment.CreateOrGetDepartment", "unable to seed ma dep");
            
            Course cscourse = csdep.GetCourse("", 499);
            if (cscourse == null)
            {
                cscourse = csdep.CreateCourse("Senior Project", 499, 2, 25);
                if (cscourse == null)
                {
                    throw new ArgumentNullException("Department.CreateCourse", "unable to create cs499");
                }
            }
            cscourse = csdep.GetCourse("", 424);
            if (cscourse == null)
            {
                cscourse = csdep.CreateCourse("Programming Languages", 424, 2, 25);
                if (cscourse == null)
                {
                    throw new ArgumentNullException("Department.CreateCourse", "unable to create cs424");
                }
            }
            Course macourse = madep.GetCourse("", 121);
            if (macourse == null)
            {
                macourse = madep.CreateCourse("Calculus A", 121, 1, 50);
                if (macourse == null)
                {
                    throw new ArgumentNullException("Department.CreateCourse", "unable to create ma121");
                }
            }
            macourse = madep.GetCourse("", 122);
            if (macourse == null)
            {
                macourse = madep.CreateCourse("Calculus B", 122, 1, 50);
                if (macourse == null)
                {
                    throw new ArgumentNullException("Department.CreateCourse", "unable to create ma122");
                }
            }
        }
    }
}
